const getCartList = () => {

  try {
    if (localStorage.cartList == undefined) {
     return [];

    }
    return JSON.parse(localStorage.cartList) || []
  } catch (e) {
    return [];
  }
}
const initCartModel= () => 
{
    let list=initShopModel();
    let cartList = [];
  list.forEach((item) => {
    let products = item.items;
    products.forEach((it) => {
      it.ischeck = false;
      it.number = 0;
    })
    cartList.push({
      shopId: item.id,
      shopName: item.name,
      items: products
    })
  })
  return cartList;
  
}
const initShopModel = () => {

  let list = [{
      id: 1,
      name: "华联超市(田村店)",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 2,
      name: "松鼠便利鲁谷店",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 3,
      name: "美团买菜(田村店)",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 4,
      name: "亿家生活超市",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 5,
      name: "每日优先(田村配送站)",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 6,
      name: "物美超市(科大店)",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 7,
      name: "京东便利店(廖公庄店)",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
    {
      id: 8,
      name: "精品水果蔬菜超市",
      //img:'http://www.dell-lee.com/imgs/vue3/near.png',
      img: require("@/assets/near.png"),
      sale: 1,
      baseprice: 5,
      startprice: 0,
      yhtext: "VIP尊享满89元减4元运费券（每月3张）",
      items: GetShopItemModel()
    },
  ]

  return list;
 
}
//设置左侧分类列表
const GetCateList = () => {
  return [
    "全部商品",
    "秒杀",
    "新鲜水果",
    "休闲食品",
    "时令蔬菜",
    "肉蛋家禽",
  ];
}
const GetShopItemModel = () => {
  let items = [{
      id: 1,
      name: "超甜橙子25…",
      oldprice: 100,
      price: 33.6,
      img: require("@/assets/tomato.png"),
    },
    {
      ischeck: false,
      id: 2,
      name: "香蕉250kg",
      oldprice: 100,
      price: 50.6,
      number: 0,
      img: require("@/assets/tomato.png"),
    },
  ]
  return items;

}
const getShopList = () => {

  try {

    if (localStorage.shopList == undefined) {
    return [];
        //   localStorage.shopList = JSON.stringify(initShopModel());
    //   //初始化购物车数据
    //   localStorage.cartList =JSON.stringify(initCartModel()) 
    }
    return JSON.parse(localStorage.shopList) || []
  } catch (e) {
    return [];
  }
}


// 不要忘记把state, mutations等暴露出去。
export default {
  namespaced: true,
  state: {
    shopList: getShopList(),
    cateList: GetCateList(),
    cartList: getCartList(),
  },
  mutations: {
    //设置购物车数据
    setCartList(state, payload) {

      let shopId = payload.shopId;
      let list = payload.list;
      //先找到店铺信息
      var index = state.cartList.findIndex((item) => {
        return item.shopId = shopId;
      })
      if (index > -1) {

        if (list.length > 0)
          state.cartList[index].items = list;
          localStorage.cartList = JSON.stringify(state.cartList)
      } 
      




    }
  },
  actions: {

  },
  getters: {

  }

}
